package spring.controller;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/mdm/api/v1/devices/{deviceId}/detail")
public class CheckInController2 {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public String authority(@PathVariable String deviceId, HttpServletRequest req, HttpServletResponse res) throws IOException {
        System.out.println("CheckInController2, deviceId = " + deviceId);

        InputStream inputStream = req.getInputStream();
        String str = IOUtils.stream2String(inputStream, "utf-8");
        System.out.println("get data = " + str); // ok

        JSONObject json = new JSONObject(str);
        json.put("result", "0");
        json.put("desc", "success");
        String detail = "{\"code\":\"2004\",\"desc\":\"succ\"}";
        json.put("detail", detail);
        String str2 = json.toString();
        System.out.println("return = " + str2); // ok

        return str2;
    }
}
